<?php

namespace Controller\Article;

class Add extends \Controller\Base {

    public function actionIndex() {
        $this->setMenuPath('Article', 'Add');
        $id = $this->getInt('id');
        $pid = $this->getInt('pid');
        if ($pid) {
            $this->setMenuPath('Category_' . $pid, 'AddArticle');
        }
        if ($this->isPost()) {
            $this->save();
        }
        if ($id) {
            $articleInfo = \Business\Article::instance()->getArticleById($id);
        }
        $category = \Data\Category::instance()->getLevelCategories(null, $pid);
        if ($category) {
            if($category[$pid]['Type'] == 0) {
                if(!empty($category[$pid]['List'])){
                    $category = current($category)['List'];
                } else {
                    $url = '/Category/Category?categoryId=' . $pid;
                    echo "<script> alert('您还没有添加分类请先添加子分类！');window.location.href='".$url."'; </script>";
                }
            }
        }
        if (empty($articleInfo)) {
            $articleInfo = array(
                'Id' => '',
                'Name' => '',
                'Title' => '',
                'Content' => '',
                'Sort' => '',
                'CategoryId' => '',
                'Cover' => '',
                'Address' => '',
                'Department' => ''
            );
        }
        $this->assign('category', $category);
        $this->assign('articleInfo', $articleInfo);
        $this->assign('id',$articleInfo['Id']);
        $this->display();
    }

    /**
     * 保存文章
     */
    private function save() {
        $data = array();
        $id = $this->postInt('id');
        $data['Name'] = $this->postParam('name');
        $data['Title'] = $this->postParam('title');
        $data['Content'] = $this->postParam('content');
        $data['Sort'] = $this->postParam('sort');
        $data['Cover'] = $this->postParam('cover');
        $data['CategoryId'] = $this->postInt('categoryId');
        $data['Address'] = $this->postParam('address');
        $data['Department'] = $this->postParam('department');
        if ($this->postInt('status')) {
            $data['Status'] = $this->postInt('status');
        } else {
            $data['Status'] = 0;
        }
        if ($id) {
            $data['UpdateTime'] =  time();
            $this->updateArticle($id, $data);
            $this->addSuccessMsg('修改成功！');
        } else {
            $data['CreateTime'] = $data['UpdateTime'] =  time();
            if ($this->addArticle($data)) {
                $this->addSuccessMsg('添加成功!');
            } else {
                $this->addWarningMsg('添加失败！');
            }
        }
    }

    /**
     * 添加文章
     *
     * @param $data 文章数组
     *
     * @return mixed 返回Id
     */
    private function addArticle($data) {
        $id =  \Business\Article::instance()->addArticle($data);
        return $id;
    }

    /**
     * 修改文章
     *
     * @param $id
     *
     * @param $data
     */
    private function updateArticle($id, $data) {
        \Business\Article::instance()->updateArticle($id, $data);
    }
}